/**
 * 
 */
package com.ei.itop.incidentmgnt.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Service;

import com.ailk.dazzle.exception.BizException;
import com.ailk.dazzle.util.AppContext;
import com.ailk.dazzle.util.ibatis.GenericDAO;
import com.ei.itop.common.bean.OpInfo;
import com.ei.itop.common.dao.CommonDAO;
import com.ei.itop.common.dbentity.CcCust;
import com.ei.itop.common.dbentity.CcUser;
import com.ei.itop.common.dbentity.IcAttach;
import com.ei.itop.common.dbentity.IcIncident;
import com.ei.itop.common.dbentity.IcTransaction;
import com.ei.itop.common.dbentity.ScJob;
import com.ei.itop.common.dbentity.ScOp;
import com.ei.itop.common.service.MailSendService;
import com.ei.itop.common.util.SessionUtil;
import com.ei.itop.custmgnt.service.CustMgntService;
import com.ei.itop.incidentmgnt.bean.IncidentInfo;
import com.ei.itop.incidentmgnt.bean.TransactionInfo;
import com.ei.itop.incidentmgnt.bean.TransactionMailMessage;
import com.ei.itop.incidentmgnt.service.AttachService;
import com.ei.itop.incidentmgnt.service.IncidentService;
import com.ei.itop.incidentmgnt.service.TransactionService;

/**
 * @author Jack.Qi
 * 
 */
@Service(value = "transactionService")
public class TransactionServiceImpl implements TransactionService {

	private static final Logger log = Logger.getLogger(TransactionServiceImpl.class);

	@Resource(name = "app.siCommonDAO")
	private GenericDAO<Long, IcTransaction> transactionDAO;

	@Resource(name = "app.siCommonDAO")
	private GenericDAO<Long, TransactionInfo> transactionInfoDAO;

	@Resource(name = "app.siCommonDAO")
	private GenericDAO<Long, ScJob> scJobDAO;

	@Resource(name = "app.siCommonDAO")
	private GenericDAO<Long, CcUser> userDAO;

	@Resource(name = "app.siCommonDAO")
	private GenericDAO<Long, ScOp> opDAO;

	@Resource(name = "commonDDLDAO")
	private CommonDAO commonDAO;

	@Resource(name = "incidentService")
	private IncidentService incidentService;

	@Resource(name = "attachService")
	private AttachService attachService;

	@Resource(name = "mailSendService")
	private MailSendService mailSendService;

	@Resource(name = "mailSendConfig")
	Map<String, String> mailSendConfig;

	@Resource(name = "custMgntService")
	private CustMgntService custMgntService;

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.ei.itop.incidentmgnt.service.ITransactionService#queryTransaction
	 * (long, long)
	 */
	public List<TransactionInfo> MBLQueryTransaction(long incidentId, OpInfo opInfo) throws Exception {
		// TODO Auto-generated method stub

		HashMap<String, Object> hm = new HashMap<String, Object>();
		hm.put("incidentId", incidentId);

		// 查询事务信息
		List<TransactionInfo> transactionInfoList = transactionInfoDAO
				.findByParams("IC_TRANSACTION.queryTransactionByIncident", hm);

		// 挨个查询事务附件，此逻辑后续可调整为一次查出全部事务的附件，在内存中匹配事务
		for (int i = 0; transactionInfoList != null && i < transactionInfoList.size(); i++) {
			TransactionInfo transactionInfo = transactionInfoList.get(i);

			List<IcAttach> attachList = attachService.getAttachList(incidentId, transactionInfo.getTransId());

			transactionInfo.setAttachList(attachList);
		}

		// 记录操作日志

		return transactionInfoList;
	}

	/**
	 * 新增事务信息
	 * 
	 * @param incidentId
	 * @param transactionInfo
	 * @param opInfo
	 * @return
	 * @throws Exception
	 */
	public long addTransaction(long incidentId, TransactionInfo transactionInfo, OpInfo opInfo) throws Exception {

		// 自动设置事件ID
		transactionInfo.setIcIncidentId(incidentId);

		// 自动设置事务创建者信息
		transactionInfo.setObjectType(opInfo.getOpType());
		transactionInfo.setObjectId(opInfo.getOpId());
		transactionInfo.setLoginCode(opInfo.getOpCode());
		transactionInfo.setObjectName(opInfo.getOpName());

		long transactionId = transactionDAO.save("IC_TRANSACTION.insert", transactionInfo);

		// 保存附件信息
		attachService.saveAttach(incidentId, transactionId, transactionInfo.getAttachList());

		return transactionId;
	}

	/**
	 * 判断是否为当前干系人
	 * 
	 * @param incident
	 * @param opType
	 * @param opId
	 * @return
	 * @throws Exception
	 */
	protected boolean isCurrentOp(IcIncident incident, String opType, long opId) throws Exception {

		// 判断是否为干系人操作
		boolean isCurrentOp = false;
		if (incident.getIcObjectType().equals(opType) && incident.getIcObjectId() == opId) {
			isCurrentOp = true;
		}

		return isCurrentOp;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.ei.itop.incidentmgnt.service.ITransactionService#
	 * userCommitTransaction (long,
	 * com.ei.itop.incidentmgnt.bean.TransactionInfo, long)
	 */
	public long MBLUserCommitTransaction(long incidentId, TransactionInfo transactionInfo, OpInfo opInfo)
			throws Exception {
		// TODO Auto-generated method stub

		// 取得事件信息
		IcIncident incident = incidentService.queryIncident(incidentId);

		// 是当前干系人，且不是完成状态
		if (isCurrentOp(incident, opInfo.getOpType(), opInfo.getOpId()) && !"8".equals(incident.getItStateCode())) {
			// 需要修改部分事件信息
			IncidentInfo ii = new IncidentInfo();

			// 调整事件干系人为事件当前处理顾问
			ii.setIcObjectType("OP");
			ii.setIcObjectId(incident.getScOpId());
			ii.setIcLoginCode(incident.getScLoginCode());
			ii.setIcObjectName(incident.getScLoginName());

			// 不需调整事件所处阶段

			// 调整事件状态
			ii.setItStateCode("3");
			ii.setItStateVal(SessionUtil.getRequestContext().getMessage("i18n.incident.mgnt.ConsultantHandleStatus"));

			// 修改事件
			incidentService.modifyIncidentAndAttach(incidentId, ii, opInfo);

			// 设置事务的事件所处阶段
			transactionInfo.setItPhase(incident.getItPhase());

			// 设置事务分类
			transactionInfo.setTransType(
					SessionUtil.getRequestContext().getMessage("i18n.incident.mgnt.FlowTransTypeNeedMore"));
		}
		// 非当前干系人
		else {
			// 不需要操作事件

			// 设置事务的事件所处阶段
			transactionInfo.setItPhase(incident.getItPhase());

			// 设置事务分类
			transactionInfo
					.setTransType(SessionUtil.getRequestContext().getMessage("i18n.incident.mgnt.NotFlowTransType"));
		}

		// 新增事务信息
		long transactionId = addTransaction(incidentId, transactionInfo, opInfo);

		// 发送邮件
		if (Boolean.parseBoolean(mailSendConfig.get("mail.allowed"))) {
			log.debug("传入参数：" + opInfo.getOpFullName() + "," + incident.getScLoginCode() + "," + incident.getCcList()
					+ "," + incident.getIncidentCode() + "," + transactionInfo.getContents());
			mailSendService.sendUserTransactionCommitMail(opInfo.getOpFullName(), opInfo.getOpCode(),
					incident.getScLoginName(), incident.getScLoginCode(),
					incident.getCcList() == null ? null : incident.getCcList().split(","), incident, transactionInfo);
		}
		// 记录操作日志
		return transactionId;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.ei.itop.incidentmgnt.service.ITransactionService#
	 * adviserCommitTransaction (long,
	 * com.ei.itop.incidentmgnt.bean.TransactionInfo, long)
	 */
	public long MBLAdviserCommitTransaction(long incidentId, TransactionInfo transactionInfo, OpInfo opInfo)
			throws Exception {
		// TODO Auto-generated method stub

		// 取得事件信息
		IcIncident incident = incidentService.queryIncident(incidentId);

		// 是当前干系人,且不是完成状态
		if (isCurrentOp(incident, opInfo.getOpType(), opInfo.getOpId()) && !"8".equals(incident.getItStateCode())) {
			// 需要修改部分事件信息
			IncidentInfo ii = new IncidentInfo();

			// 不需调整事件干系人

			// 不需调整事件所处阶段

			// 调整事件状态
			ii.setItStateCode("3");
			ii.setItStateVal(SessionUtil.getRequestContext().getMessage("i18n.incident.mgnt.ConsultantHandleStatus"));

			// 修改事件
			incidentService.modifyIncidentAndAttach(incidentId, ii, opInfo);

			// 设置事务的事件所处阶段
			transactionInfo.setItPhase(incident.getItPhase());

			// 设置事务分类
			transactionInfo.setTransType(
					SessionUtil.getRequestContext().getMessage("i18n.incident.mgnt.FlowTransTypeConsultantHandle"));
		}
		// 非当前干系人
		else {
			// 不需要操作事件

			// 设置事务的事件所处阶段
			transactionInfo.setItPhase(incident.getItPhase());

			// 设置事务分类
			transactionInfo
					.setTransType(SessionUtil.getRequestContext().getMessage("i18n.incident.mgnt.NotFlowTransType"));
		}

		// 新增事务信息
		long transactionId = addTransaction(incidentId, transactionInfo, opInfo);

		// 发送邮件
		if (Boolean.parseBoolean(mailSendConfig.get("mail.allowed"))) {
			log.debug("传入参数：" + opInfo.getOpFullName() + "," + incident.getIcLoginCode() + "," + incident.getCcList()
					+ "," + incident.getIncidentCode() + "," + transactionInfo.getContents());

			mailSendService.sendAdviserTransactionCommitMail(incident.getIcOwnerName(), incident.getIcOwnerCode(),
					incident.getScLoginName(), incident.getScLoginCode(),
					incident.getCcList() == null ? null : incident.getCcList().split(","), incident, transactionInfo);
		}
		// 记录操作日志
		return transactionId;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.ei.itop.incidentmgnt.service.ITransactionService#needAdditionalInfo
	 * (long, com.ei.itop.incidentmgnt.bean.TransactionInfo, long)
	 */
	public long MBLNeedAdditionalInfo(long incidentId, TransactionInfo transactionInfo, OpInfo opInfo)
			throws Exception {
		// TODO Auto-generated method stub

		// 取得事件信息
		IcIncident incident = incidentService.queryIncident(incidentId);

		// 是当前干系人
		if (isCurrentOp(incident, opInfo.getOpType(), opInfo.getOpId())) {
			// 需要修改部分事件信息
			IncidentInfo ii = new IncidentInfo();

			// 需调整事件干系人为事件所属人
			ii.setIcObjectType(incident.getIcOwnerType());
			ii.setIcObjectId(incident.getIcOwnerId());
			ii.setIcLoginCode(incident.getIcOwnerCode());
			ii.setIcObjectName(incident.getIcOwnerName());

			// 不需调整事件所处阶段

			// 需调整事件状态为客户处理中
			ii.setItStateCode("4");
			ii.setItStateVal(SessionUtil.getRequestContext().getMessage("i18n.incident.mgnt.UserHandleStatus"));

			// 修改事件
			incidentService.modifyIncidentAndAttach(incidentId, ii, opInfo);

			// 设置事务的事件所处阶段
			transactionInfo.setItPhase(incident.getItPhase());

			// 设置事务分类
			transactionInfo.setTransType(
					SessionUtil.getRequestContext().getMessage("i18n.incident.mgnt.FlowTransTypeNeedMore"));
		}
		// 非当前干系人
		else {
			throw new BizException(
					SessionUtil.getRequestContext().getMessage("i18n.incident.mgnt.OnlyRespConsultantAskNeedMore"));
		}

		// 新增事务信息
		long transactionId = addTransaction(incidentId, transactionInfo, opInfo);

		// 发送邮件
		if (Boolean.parseBoolean(mailSendConfig.get("mail.allowed"))) {
			log.debug("传入参数：" + incident.getIcOwnerName() + "," + opInfo.getOpFullName() + "," + opInfo.getOpCode()
					+ "," + incident.getIcOwnerCode() + "," + incident.getCcList() + "," + incident.getIncidentCode()
					+ "," + transactionInfo.getContents());
			mailSendService.sendTransactionNeedMoreInfoMail(incident.getIcOwnerName(), opInfo.getOpFullName(),
					opInfo.getOpCode(), incident.getIcOwnerCode(),
					incident.getCcList() == null ? null : incident.getCcList().split(","), incident, transactionInfo);
		}

		// 记录操作日志

		return transactionId;
	}
	/**
	 * 判断是否是当前事件的事件经理
	 * @param incident
	 * @param opInfo
	 * @return
	 * @throws Exception
	 */
	private boolean isIncidentManager(IcIncident incident,OpInfo opInfo) throws Exception{
		// 取得当前人岗位信息
		IncidentInfo incidentInfo = new IncidentInfo();
		incidentInfo.setIcIncidentId(incident.getIcIncidentId());
		incidentInfo.setScProductId(incident.getScProductId());
		incidentInfo.setScOrgId(incident.getScOrgId());
		incidentInfo.setCcCustId(incident.getCcCustId());
		List<ScJob> jobs = incidentService.MBLQueryJobByIncident(incidentInfo,opInfo.getOpId());
		if(jobs!=null&&jobs.size()>0){
			for(int i=0;i<jobs.size();i++){
				if("9".equals(jobs.get(i).getJobLevel())){
					return true;
				}
			}
		}
		return false;
	}
	
	/*
	 * (non-Javadoc)
	 * 
	 * @see com.ei.itop.incidentmgnt.service.ITransactionService#
	 * adviserTransferTransaction(long,
	 * com.ei.itop.incidentmgnt.bean.TransactionInfo, long)
	 */
	public long MBLAdviserTransferTransaction(long incidentId, TransactionInfo transactionInfo, OpInfo nextOpInfo,
			OpInfo opInfo) throws Exception {
		// TODO Auto-generated method stub

		// 取得事件信息
		IcIncident incident = incidentService.queryIncident(incidentId);
		
		// 是当前干系人或者是事件经理
		if (isCurrentOp(incident, opInfo.getOpType(), opInfo.getOpId())||isIncidentManager(incident,opInfo)) {
			// 需要修改部分事件信息
			IncidentInfo ii = new IncidentInfo();

			// 需调整事件干系人为被转派人
			ii.setIcObjectId(nextOpInfo.getOpId());
			ii.setIcLoginCode(nextOpInfo.getOpCode());
			ii.setIcObjectName(nextOpInfo.getOpName());

			// 需调整负责顾问为被转派人
			ii.setScOpId(nextOpInfo.getOpId());
			ii.setScLoginCode(nextOpInfo.getOpCode());
			ii.setScLoginName(nextOpInfo.getOpName());

			// 需调整事件所处阶段
			String itPhase = incidentService.getItPhase(incident.getScOrgId(), incident.getCcCustId(),
					incident.getScProductId(), nextOpInfo.getOpId());
			ii.setItPhase(itPhase);

			// 需调整事件状态为处理中
			ii.setItStateCode("3");
			ii.setItStateVal(SessionUtil.getRequestContext().getMessage("i18n.incident.mgnt.ConsultantHandleStatus"));

			// 修改事件
			incidentService.modifyIncidentAndAttach(incidentId, ii, opInfo);

			// 设置事务的事件所处阶段
			transactionInfo.setItPhase(itPhase);

			// 设置事务分类
			transactionInfo.setTransType(
					SessionUtil.getRequestContext().getMessage("i18n.incident.mgnt.FlowTransTypeHandover"));
		}
		// 非当前干系人
		else {
			throw new BizException(
					SessionUtil.getRequestContext().getMessage("i18n.incident.mgnt.OnlyRespConsultantCanHandover"));
		}

		// 新增事务信息
		long transactionId = addTransaction(incidentId, transactionInfo, opInfo);

		// 发送邮件
		if (Boolean.parseBoolean(mailSendConfig.get("mail.allowed"))) {
			log.debug("传入参数：" + nextOpInfo.getOpName() + "," + opInfo.getOpFullName() + "," + opInfo.getOpCode() + ","
					+ nextOpInfo.getOpCode() + "," + incident.getCcList() + "," + incident.getIncidentCode() + ","
					+ transactionInfo.getContents());

			mailSendService.sendTransactionTransferMail(nextOpInfo.getOpName(), opInfo.getOpFullName(),
					incident.getIcOwnerName(), opInfo.getOpCode(), nextOpInfo.getOpCode(), incident.getIcOwnerCode(),
					incident.getCcList() == null ? null : incident.getCcList().split(","), incident, transactionInfo);

		}
		// 记录操作日志
		return transactionId;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.ei.itop.incidentmgnt.service.ITransactionService#
	 * adviserHangUpTransaction (long,
	 * com.ei.itop.incidentmgnt.bean.TransactionInfo, long)
	 */
	public long MBLAdviserHangUpTransaction(long incidentId, TransactionInfo transactionInfo, OpInfo opInfo)
			throws Exception {
		// TODO Auto-generated method stub

		// 取得事件信息
		IcIncident incident = incidentService.queryIncident(incidentId);

		// 只有2-待响应、3-顾问处理中的事件才能挂起
		if (!("2".equals(incident.getItStateCode()) || "3".equals(incident.getItStateCode()))) {
			throw new BizException(
					SessionUtil.getRequestContext().getMessage("i18n.incident.mgnt.BlockTicketConditionError"));
		}

		// 是当前干系人
		if (isCurrentOp(incident, opInfo.getOpType(), opInfo.getOpId())) {
			// 需要修改部分事件信息
			IncidentInfo ii = new IncidentInfo();

			// 不需调整事件干系人为被转派人

			// 不需调整负责顾问为备注安排人

			// 不需调整事件所处阶段

			// 需调整事件状态为已挂起
			ii.setItStateCode("5");
			ii.setItStateVal(SessionUtil.getRequestContext().getMessage("i18n.incident.mgnt.BlockStatus"));

			// 修改事件
			incidentService.modifyIncidentAndAttach(incidentId, ii, opInfo);

			// 设置事务的事件所处阶段
			transactionInfo.setItPhase(incident.getItPhase());

			// 设置事务分类
			transactionInfo
					.setTransType(SessionUtil.getRequestContext().getMessage("i18n.incident.mgnt.FlowTransTypeBlock"));
		}
		// 非当前干系人
		else {
			throw new BizException(
					SessionUtil.getRequestContext().getMessage("i18n.incident.mgnt.OnlyRespConsultantCanBlock"));
		}

		// 新增事务信息
		long transactionId = addTransaction(incidentId, transactionInfo, opInfo);

		// 发送邮件
		if (Boolean.parseBoolean(mailSendConfig.get("mail.allowed"))) {
			log.debug("传入参数：" + opInfo.getOpFullName() + "," + opInfo.getOpCode() + "," + incident.getCcList() + ","
					+ incident.getIncidentCode() + "," + transactionInfo.getContents());

			mailSendService.sendTransactionBlockMail(incident.getIcOwnerName(), incident.getIcOwnerCode(),
					opInfo.getOpFullName(), opInfo.getOpCode(),
					incident.getCcList() == null ? null : incident.getCcList().split(","), incident, transactionInfo);
		}

		// 记录操作日志

		return transactionId;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.ei.itop.incidentmgnt.service.ITransactionService#
	 * adviserCompleteTransaction(long,
	 * com.ei.itop.incidentmgnt.bean.IncidentInfo,
	 * com.ei.itop.incidentmgnt.bean.TransactionInfo, long)
	 */
	public long MBLAdviserCompleteTransaction(long incidentId, IncidentInfo incidentInfo,
			TransactionInfo transactionInfo, OpInfo opInfo) throws Exception {
		// TODO Auto-generated method stub

		// 取得事件信息
		IcIncident incident = incidentService.queryIncident(incidentId);

		// 只有3-顾问处理中的事件才能设置事件完成
		if (!"3".equals(incident.getItStateCode())) {
			throw new BizException(
					SessionUtil.getRequestContext().getMessage("i18n.incident.mgnt.OnlyDealingTicketCanFinish"));
		}

		// 判断时间结束代码是否填写
		if (incidentInfo.getFinishCode() == null || "".equals(incidentInfo.getFinishCode())
				|| incidentInfo.getFinishVal() == null || "".equals(incidentInfo.getFinishVal())) {
			throw new BizException("必须填写事件结束代码");
		}

		// 是当前干系人
		if (isCurrentOp(incident, opInfo.getOpType(), opInfo.getOpId())) {
			// 需要修改部分事件信息
			IncidentInfo ii = new IncidentInfo();

			// 不需调整事件干系人

			// 不需调整负责顾问

			// 不需调整事件所处阶段

			// 需调整事件状态为已挂起
			ii.setItStateCode("8");
			ii.setItStateVal(SessionUtil.getRequestContext().getMessage("i18n.incident.mgnt.FinishStatus"));

			// 需补全完成操作的业务信息
			ii.setFinishTime(commonDAO.getSysDate());
			ii.setFinishCode(incidentInfo.getFinishCode());
			ii.setFinishVal(incidentInfo.getFinishVal());
			ii.setRestoreTime(incidentInfo.getRestoreTime());
			ii.setItSolution(incidentInfo.getItSolution());
			ii.setIncidentReason(incidentInfo.getIncidentReason());
			// 填入实际响应时长、实际处理时长等信息
			ii.setDealDur(incidentInfo.getDealDur());

			// 修改事件
			incidentService.modifyIncidentAndAttach(incidentId, ii, opInfo);

			// 设置事务的事件所处阶段
			transactionInfo.setItPhase(incident.getItPhase());

			// 设置事务分类
			transactionInfo
					.setTransType(SessionUtil.getRequestContext().getMessage("i18n.incident.mgnt.FlowTransTypeFinish"));
		}
		// 非当前干系人
		else {
			throw new BizException(
					SessionUtil.getRequestContext().getMessage("i18n.incident.mgnt.OnlyRespConsultantCanFinish"));
		}

		// 新增事务信息
		long transactionId = addTransaction(incidentId, transactionInfo, opInfo);

		// 发送邮件
		if (Boolean.parseBoolean(mailSendConfig.get("mail.allowed"))) {
			log.debug("传入参数：" + opInfo.getOpFullName() + "," + opInfo.getOpCode() + "," + incident.getCcList() + ","
					+ incident.getIncidentCode() + "," + transactionInfo.getContents());
			mailSendService.sendTransactionFinishMail(incident.getIcOwnerName(), incident.getIcOwnerCode(),
					opInfo.getOpFullName(), opInfo.getOpCode(),
					incident.getCcList() == null ? null : incident.getCcList().split(","), incident, transactionInfo);
		}

		// 记录操作日志

		return transactionId;
	}

	public static void main(String[] args) throws Exception {
		TransactionService ts = (TransactionService) AppContext.getBean("transactionService");

		OpInfo oi = new OpInfo();
		oi.setOpType("OP");
		oi.setOpId(0);
		oi.setOpCode("SamZhang@163.com");
		oi.setOpName("Sam.Zhang（张三）");

		// ts.MBLQueryTransaction(10027, oi);

		TransactionInfo ti = new TransactionInfo();
		ti.setContents("事务内容XXX");
		List<IcAttach> attachList = new ArrayList<IcAttach>();
		IcAttach attach1 = new IcAttach();
		attach1.setAttachPath("/data/a1.txt");
		IcAttach attach2 = new IcAttach();
		attach2.setAttachPath("/data/b1.doc");
		attachList.add(attach1);
		attachList.add(attach2);
		ti.setAttachList(attachList);

		// ts.addTransaction(10026, ti, oi);

		// 用户提交事务-非流程
		// oi.setOpType("USER");
		// ts.MBLUserCommitTransaction(10026, ti, oi);

		// 顾问提交事务-非流程
		// ts.MBLAdviserCommitTransaction(10026, ti, oi);
		// 顾问提交事务-流程
		// oi.setOpType("OP");
		// oi.setOpId(200005);
		// oi.setOpCode("SP200005");
		// oi.setOpName("EI-PM");
		// ts.MBLAdviserCommitTransaction(10026, ti, oi);

		// 需补全资料
		// oi.setOpType("OP");
		// oi.setOpId(200005);
		// oi.setOpCode("SP200005");
		// oi.setOpName("EI-PM");
		// ts.MBLNeedAdditionalInfo(10026, ti, oi);

		// 用户提交事务-非流程
		// oi.setOpType("USER");
		// ts.MBLUserCommitTransaction(10026, ti, oi);

		// 用户提交事务-流程
		// oi.setOpType("USER");
		// oi.setOpId(9001);
		// oi.setOpCode("ITOPCNH1@163.com");
		// oi.setOpName("itop1-cnh");
		// ts.MBLUserCommitTransaction(10026, ti, oi);

		// 顾问转派事务
		// oi.setOpType("OP");
		// oi.setOpId(200005);
		// oi.setOpCode("SP200005");
		// oi.setOpName("EI-PM");
		// OpInfo nextOpInfo = new OpInfo();
		// nextOpInfo.setOpType("OP");
		// nextOpInfo.setOpId(200002);
		// nextOpInfo.setOpCode("SP200002");
		// nextOpInfo.setOpName("ITOPEI2-EI");
		// ts.MBLAdviserTransferTransaction(10026, ti, nextOpInfo, oi);

		// 顾问挂起事务
		// oi.setOpType("OP");
		// oi.setOpId(200002);
		// oi.setOpCode("SP200002");
		// oi.setOpName("ITOPEI2-EI");
		// ts.MBLAdviserHangUpTransaction(10026, ti, oi);

		// 顾问转派事务
		// oi.setOpType("OP");
		// oi.setOpId(200002);
		// oi.setOpCode("SP200002");
		// oi.setOpName("ITOPEI2-EI");
		// OpInfo nextOpInfo = new OpInfo();
		// nextOpInfo.setOpType("OP");
		// nextOpInfo.setOpId(200005);
		// nextOpInfo.setOpCode("SP200005");
		// nextOpInfo.setOpName("EI-PM");
		// ts.MBLAdviserTransferTransaction(10026, ti, nextOpInfo, oi);

		// // 顾问挂起事务
		// oi.setOpType("OP");
		// oi.setOpId(200005);
		// oi.setOpCode("SP200005");
		// oi.setOpName("EI-PM");
		// ts.MBLAdviserHangUpTransaction(10026, ti, oi);

		// // 顾问提交事务-非流程
		// ts.MBLAdviserCommitTransaction(10026, ti, oi);

		// 顾问提交事务-流程
		// oi.setOpType("OP");
		// oi.setOpId(200005);
		// oi.setOpCode("SP200005");
		// oi.setOpName("EI-PM");
		// ts.MBLAdviserCommitTransaction(10026, ti, oi);

		// 顾问完成事务
		// IncidentInfo incidentInfo = new IncidentInfo();
		// incidentInfo.setFinishCode("4");
		// incidentInfo.setFinishVal("消失");
		// oi.setOpType("OP");
		// oi.setOpId(200005);
		// oi.setOpCode("SP200005");
		// oi.setOpName("EI-PM");
		// ts.MBLAdviserCompleteTransaction(10026, incidentInfo, ti, oi);
	}

	public void MBLResetAdviserProcessTransaction(long incidentId, TransactionInfo ti, OpInfo opInfo) throws Exception {
		// 取得事件信息
		IcIncident incident = incidentService.queryIncident(incidentId);

		// 只有4-客户处理中的事件才可以重置为顾问处理中
		if (!"4".equals(incident.getItStateCode())) {
			throw new BizException(
					SessionUtil.getRequestContext().getMessage("i18n.incident.mgnt.ResetAdviserProcessError"));
		}

		// 判断是否是事件经理
		HashMap<String, Object> hm = new HashMap<String, Object>();
		hm.put("opId", opInfo.getOpId());
		hm.put("prodId", incident.getScProductId());
		hm.put("orgId", incident.getScOrgId());
		CcCust custInfo = custMgntService.getCustInfo(incident.getCcCustId());
		hm.put("custId",
				custMgntService.getTopCustInfo(custInfo.getCcCustId(), custInfo.getDomainName()).getCcCustId());
		List<ScJob> jobs = scJobDAO.findByParams("SC_JOB.selectByIncident", hm);
		boolean isTicketManager = false;
		for (ScJob job : jobs) {
			if ("9".equals(job.getJobLevel())) {
				isTicketManager = true;
				break;
			}
		}
		// 非事件经理抛出异常
		if (!isTicketManager) {
			throw new BizException(
					SessionUtil.getRequestContext().getMessage("i18n.incident.mgnt.ResetAdviserProcessAuthError"));
		}
		// 需要修改部分事件信息
		IncidentInfo ii = new IncidentInfo();
		// 修改干系人为当前顾问
		ii.setIcObjectType("OP");
		ii.setIcObjectId(ii.getScOpId());
		ii.setIcObjectName(ii.getScLoginName());
		ii.setIcLoginCode(ii.getScLoginCode());
		// 需调整事件状态为顾问处理中
		ii.setItStateCode("3");
		ii.setItStateVal(SessionUtil.getRequestContext().getMessage("i18n.incident.mgnt.ConsultantHandleStatus"));
		// 修改事件
		incidentService.modifyIncidentAndAttach(incidentId, ii, opInfo);
		// 设置事务的事件所处阶段
		ti.setItPhase(incident.getItPhase());
		// 设置事务分类
		ti.setTransType(SessionUtil.getRequestContext().getMessage("i18n.incident.mgnt.FlowTransTypeConsultantHandle"));
		// 新增事务信息
		addTransaction(incidentId, ti, opInfo);
	}

	public void MBLUploadReportTransaction(long incidentId, TransactionInfo ti, OpInfo opInfo) throws Exception {
		// 取得事件信息
		IcIncident incident = incidentService.queryIncident(incidentId);
		// 需要修改部分事件信息
		IncidentInfo ii = new IncidentInfo();
		ii.setIcIncidentId(incidentId);
		ii.setReportAttachId(ti.getAttachList().get(0).getIcAttachId());
		// 修改事件
		incidentService.modifyIncidentAndAttach(incidentId, ii, opInfo);
		// 设置事务的事件所处阶段
		ti.setItPhase(incident.getItPhase());
		// 设置事务分类
		ti.setTransType(SessionUtil.getRequestContext().getMessage("i18n.incident.mgnt.NotFlowTransType"));
		// 新增事务信息
		addTransaction(incidentId, ti, opInfo);

	}

	public void MBLAutoCommitTransaction(List<TransactionMailMessage> commitMsg) {
		for(TransactionMailMessage tm:commitMsg){
			try{
				//查询事件
				IncidentInfo ii = incidentService.queryIncidentByCode(tm.getIncidentNo());
				//如果事件不存在或事件状态不再允许提交事务
				if(ii==null||ii.getItStateCode()=="1"||ii.getItStateCode()=="9"||ii.getItStateCode()=="91"){
					continue;
				}
				//生成事务
				TransactionInfo ti = new TransactionInfo();
				ti.setIcIncidentId(ii.getIcIncidentId());
				ti.setContents(tm.getTransContent());
				//查询得到用户还是顾问
				HashMap<String, Object> hm = new HashMap<String, Object>();
				hm.put("loginCode", tm.getUserAccount());
				CcUser user = userDAO.find("CC_USER.queryUserByLoginCode", hm);
				//如果是用户，且正常状态
				if (user != null&&user.getState() == 1) {
					OpInfo opInfo = new OpInfo();
					opInfo.setOpId(user.getCcUserId());
					opInfo.setOpType("USER");
					opInfo.setOpCode(user.getLoginCode());
					opInfo.setOpFullName(user.getLastName() + "." + user.getFirstName());
					opInfo.setOpName(user.getOpName());
					userAutoCommitTransaction(ti, opInfo);
				}else{
					ScOp op = opDAO.find("SC_OP.queryOpByLoginCode", hm);
					//如果是顾问，且正常状态
					if(op!=null&&op.getState()==1){
						OpInfo opInfo = new OpInfo();
						opInfo.setOpId(op.getScOpId());
						opInfo.setOpType("OP");
						opInfo.setOpCode(op.getLoginCode());
						opInfo.setOpFullName(op.getLastName() + "." + op.getFirstName());
						opInfo.setOpName(op.getOpName());
						adviserCommitTransaction(ti, opInfo);
					}
				}
			}catch(Exception e){
				log.error("处理自动提交消息失败",e);
			}
		}
	}
	
	private void userAutoCommitTransaction(TransactionInfo transactionInfo, OpInfo opInfo)
			throws Exception {
		// 取得事件信息
		IcIncident incident = incidentService.queryIncident(transactionInfo.getIcIncidentId());
		transactionInfo.setItPhase(incident.getItPhase());
		transactionInfo.setTransType("Other Flow");
		// 新增事务信息
		addTransaction(transactionInfo.getIcIncidentId(),transactionInfo, opInfo);
		// 发送邮件
		if (Boolean.parseBoolean(mailSendConfig.get("mail.allowed"))) {
			log.debug("传入参数：" + opInfo.getOpFullName() + "," + incident.getScLoginCode() + "," + incident.getCcList()
					+ "," + incident.getIncidentCode() + "," + transactionInfo.getContents());
			mailSendService.sendUserTransactionCommitMail(opInfo.getOpFullName(), opInfo.getOpCode(),
					incident.getScLoginName(), incident.getScLoginCode(),
					incident.getCcList() == null ? null : incident.getCcList().split(","), incident, transactionInfo);
		}
	}
	
	private void adviserCommitTransaction(TransactionInfo transactionInfo, OpInfo opInfo)
			throws Exception {
		// 取得事件信息
		IcIncident incident = incidentService.queryIncident(transactionInfo.getIcIncidentId());
		transactionInfo.setItPhase(incident.getItPhase());
		transactionInfo.setTransType("其他事务");
		// 是当前干系人,且事件为待处理状态
		if (isCurrentOp(incident, opInfo.getOpType(), opInfo.getOpId()) && "2".equals(incident.getItStateCode())) {
			// 需要修改部分事件信息
			IncidentInfo ii = new IncidentInfo();
			ii.setIcIncidentId(incident.getIcIncidentId());
			ii.setItStateCode("3");
			ii.setItStateVal("处理中");
			// 修改事件
			incidentService.modifyIncidentAndAttach(incident.getIcIncidentId(), ii, opInfo);
			// 设置事务的事件所处阶段
			transactionInfo.setItPhase(incident.getItPhase());
			// 设置事务分类
			transactionInfo.setTransType("流程事务-顾问处理中");
		}
		// 新增事务信息
		addTransaction(transactionInfo.getIcIncidentId(), transactionInfo, opInfo);
		// 发送邮件
		if (Boolean.parseBoolean(mailSendConfig.get("mail.allowed"))) {
			log.debug("传入参数：" + opInfo.getOpFullName() + "," + incident.getIcLoginCode() + "," + incident.getCcList()
					+ "," + incident.getIncidentCode() + "," + transactionInfo.getContents());
			mailSendService.sendAdviserTransactionCommitMail(incident.getIcOwnerName(), incident.getIcOwnerCode(),
					incident.getScLoginName(), incident.getScLoginCode(),
					incident.getCcList() == null ? null : incident.getCcList().split(","), incident, transactionInfo);
		}
	}
}
